package com.lancoo.examview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * Created by pengmi on 2022/6/21 20:54
 * Description:
 */
public class PracticeItemAdapter extends RecyclerView.Adapter<PracticeItemAdapter.VH> {

    private List<String> contentList;

    private OnPaperOnClickCallBack onCallBack;

    public PracticeItemAdapter(List<String> contentList) {
        this.contentList = contentList;
    }

    public void setOnCallBack(OnPaperOnClickCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_practice, parent, false);
        return new VH(inflate,onCallBack);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        holder.initData(contentList.get(position));
    }

    @Override
    public int getItemCount() {
        return contentList==null?0:contentList.size();
    }

    public static class VH extends RecyclerView.ViewHolder implements View.OnClickListener{
        private final OnPaperOnClickCallBack onCallBack;
        private String name;

        public VH(@NonNull View itemView,OnPaperOnClickCallBack onCallBack) {
            super(itemView);
            this.onCallBack=onCallBack;
            itemView.setOnClickListener(this);
        }

        private void initData(String content){
            name=content;
            Button btn = itemView.findViewById(R.id.btn);
            btn.setText(content);
        }

        @Override
        public void onClick(View v) {
            if(onCallBack!=null){
                onCallBack.onClick(name);
            }
        }
    }

    public interface OnPaperOnClickCallBack{
        void onClick(String paperName);
    }
}
